home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Utilities ƒ / MPW Tools ƒ / Simula4.07 / Simula 4.07ƒ / SExamples / processes.sim < prev    next >
Encoding:
Text File  |  1989-05-02  |  6.7 KB  |  251 lines  |  [TEXT/MPS ]

  1. % ==================================================
  2. % File: Processes
  3. % This Example demonstrate how you can dynamically create 
  4. % windows and processes to control them. Notice how in this 
  5. % case multiple instances of the same class "Controller"
  6. % are designed to handle only events to its own Window.
  7. %  The Windows are of different kinds and behaves 
  8. % differently due to the virtual procedure "Draw".
  9. % Notice also that different objects of the same class
  10. % "CreatItem" are used in the create menu. They distinguish
  11. % their actions by using the parameters to the virtual
  12. % procedure "doMenu".
  13. % The macProcessMGR schedules the process instances and
  14. % does all the book keeping before activating them.
  15. % Compile: simcomp processes
  16. % Link:    simld   processes -toolbox -APPL
  17. % This sequence can be generated by entering "make processes"
  18. %
  19. % Using: create process with the "create" menu.
  20. %        Click them to make them draw. See how they
  21. %        are updated when overlapping, moved and resized
  22. % =====================================================
  23. begin
  24. external class macRect="::sinterfaces:macRect";
  25. external class macProcessmgr="::sinterfaces:macprocessmgr";
  26. external class macProcess="::sinterfaces:macprocess";
  27. external class macmenubar="::sinterfaces:macmenubar";
  28. external class defaultmenuprocess="::sinterfaces:defaultmenuprocess";
  29. external class macmenu="::sinterfaces:macmenu";
  30. external class macmenuitem="::sinterfaces:macmenuitem";
  31. external class defaultAppleMenu="::Sinterfaces:defaultAppleMenu";
  32.  
  33. !--------------------------------------------;
  34. ! Windows 
  35. !--------------------------------------------;
  36.     ! Here MacWindow is used as super class to several
  37.     ! specializations. They differ by different
  38.     ! implementations of the virtual procedure "Draw".
  39.     ! The sub class of macProcess: "Controller" need
  40.     ! only know about "RandomWindow":s 
  41.     !---------------------------;
  42.     macwindow class randomwindow(seed,Width,Height);integer Seed,Width,Height;
  43.         virtual: procedure draw is
  44.             procedure draw;;
  45.     begin
  46.         integer Count; ! Updated by "Draw" each time it is called;
  47.         integer InitialSeed;
  48.         ! ReDraw is called when the window has to be updated ;
  49.         procedure ReDraw;
  50.         begin
  51.             integer i,todo;
  52.             seed:=InitialSeed;
  53.             todo:=count;
  54.             count:=0;
  55.             for i:=1 step 1 until todo do
  56.                 Draw;
  57.         end;
  58.         initialseed:=seed;
  59.     end;
  60.  
  61.     RandomWindow class RandomDraw;
  62.     begin
  63.         procedure Draw;
  64.         begin
  65.             integer dx,dy,i;
  66.             PenNormal;
  67.             moveto(Width/2,Height/2);
  68.             for i:=1 step 1 until 10 do
  69.             begin
  70.                 dx:=randint(0,20,seed)-10;
  71.                 dy:=randint(0,40,seed)-20;
  72.                 line(dx,dy);
  73.             end;
  74.             Count:=Count+1;
  75.         end --- Draw ---;
  76.     end --- Randow Draw --- ;
  77.     
  78.     randomWindow class RandomText;
  79.     begin
  80.         procedure Draw;
  81.         begin
  82.             integer x,y,s,f,i,ch;
  83.             PenNormal;
  84.             for i:=1 step 1 until 10 do
  85.             begin
  86.                 x:=randint(0,Width,Seed)-5;
  87.                 y:=randint(0,Height,Seed)-10;
  88.                 ch:=randint(rank('a'),rank('z'),Seed);
  89.                 moveto(x,y);
  90.                 drawchar(char(ch));
  91.             end;
  92.             Count:=Count+1;
  93.         end --- Draw ---;
  94.         
  95.     end --- Randow Text --- ;
  96.  
  97.     randomWindow class RandomFont;
  98.     begin
  99.         procedure Draw;
  100.         begin
  101.             integer x,y,s,f,i,ch;                
  102.             PenNormal;
  103.             s:=randint(9,20,Seed);
  104.             f:=randint(0,24,Seed);
  105.             textfont(f);
  106.             textsize(s);
  107.             for i:=1 step 1 until 10 do
  108.             begin
  109.                 x:=randint(0,Width,Seed)-5;
  110.                 y:=randint(0,Height,Seed)-10;
  111.                 ch:=randint(rank('a'),rank('z'),Seed);
  112.                 moveto(x,y);
  113.                 drawchar(char(ch));
  114.             end;
  115.             Count:=Count+1;
  116.         end --- Draw ---;
  117.     end --- Randow Font --- ;
  118. !--------------------------------------------;
  119. ! Process
  120. !--------------------------------------------;
  121.     ! Controller: is designed to control any 
  122.     ! subclass of class randomWindow.
  123.     !---------------------------;
  124.     macProcess  class Controller(W); ref(randomWindow) W; 
  125.     begin
  126.         ! Called when the User have touched the "res-size"
  127.         ! button.
  128.         !---------------;
  129.         procedure doSize(width,height); integer width,height;
  130.         begin
  131.             W.SizeWindow(width,height,true);
  132.         end;
  133.         ! Called when the user have clicked the "goAway" button.
  134.         !-----------------;
  135.         procedure doGoAway;
  136.         begin
  137.             W.closewindow;
  138.             theMGR.kill(this controller);
  139.         end;
  140.         ! --- main loop of the process -- ;
  141.         ref(macEvent) E;
  142.         integer i;
  143.         E:- new macEvent;
  144.         EnableEvent(Tconst.mouseDown);
  145.         EnableEvent(TConst.updateEvt);
  146.         while true do
  147.         begin
  148.             waitnextevent(e);
  149.             if e.what=Tconst.updateEvt then
  150.             begin
  151.                 W.beginUpdate;
  152.                 W.reDraw;
  153.                 W.EndUpdate;
  154.             end
  155.             else ! - Mouse down event - ;
  156.                 W.Draw;
  157.         end;
  158.     end --- Controller --;
  159. !--------------------------------------------;
  160. ! Menu classes
  161. !--------------------------------------------;
  162.     ! Almost the same thing should be done
  163.     ! when any of the window-types are created
  164.     ! We therefore define one CreateItem class
  165.     ! that checks the "i" parameter to create a
  166.     ! Window of correct type. An alternative
  167.     ! is to define one Item-class for each
  168.     ! alternative.
  169.     !---------------------------;
  170.     macmenuItem class CreateItem;
  171.     begin
  172.         procedure doMenu(m,i); integer m,i;
  173.         begin
  174.             ref(randomWindow) w1;
  175.             ref(macrect) Wbounds;
  176.             text Title;
  177.             if i=1 then
  178.             begin
  179.                 w1:- new RandomDraw(GlobalSeed,200,200);
  180.                 Title:-"Random Drawing";
  181.             end
  182.             else if i=2 then
  183.             begin
  184.                 w1:- new RandomText(GlobalSeed,200,200);
  185.                 Title:-"Random Text";
  186.             end
  187.             else 
  188.             begin
  189.                 w1:- new RandomFont(GlobalSeed,200,200);
  190.                 Title:-"Random Font";
  191.                 disableItem; ! -- one Font-Window is enough -;
  192.             end;
  193.             Wbounds:-new macRect;
  194.             inspect Wbounds do
  195.             begin
  196.                 top:=Randint(50,250,GlobalSeed);    bottom:=top+W1.Width;
  197.                 left:=Randint(50,250,GlobalSeed);   right:=left+W1.Height;
  198.             end;
  199.             W1.NewWindow(Wbounds,Title,true,0,none,true,0);
  200.             theMGR.RegisterWindow(w1);
  201.             theMGR.RegisterProcess(new Controller(w1),w1);
  202.         end;
  203.     end --- Create Item --- ;
  204.  
  205.     macmenuitem class quitItem;
  206.     begin
  207.         procedure doMenu(m,i); integer m,i;
  208.             theMGR.stop;
  209.     end --- quitItem --- ;
  210.  
  211.     ref(macProcessMgr) theMGR;
  212.     ref(macmenubar) menubar;
  213.     ref(macmenu) menu1,menu2;
  214.     ref(macprocess) p;
  215.     integer Globalseed;
  216.  
  217.     Globalseed:=1147;
  218.     ! -- One each of these classes -- ;
  219.     theMGR:- new macprocessmgr;
  220.     menubar:- new macmenubar;
  221.     P:- new defaultmenuprocess(Menubar);
  222.     theMGR.RegisterMenuProcess(P);
  223.  
  224.     ! -- Set up the menu -- ;
  225.     menubar.insertmenu(new defaultApplemenu,0);
  226.  
  227.     Menu2:-new MacMenu;
  228.         Menu2.newmenu(133,"File");
  229.         menubar.insertmenu(Menu2,0);
  230.         Menu2.appendmenu("quit/Q");
  231.         menu2.registeritem(new QuitItem,1);
  232.     Menu1:-new macMenu;
  233.         Menu1.newMenu(132,"Create");
  234.         menubar.insertmenu(Menu1,0);
  235.         Menu1.appendmenu("Draw/D");
  236.         Menu1.appendmenu("Text");
  237.         Menu1.appendmenu("Font");
  238.         Menu1.RegisterItem(new CreateItem,1);
  239.         Menu1.RegisterItem(new CreateItem,2);
  240.         Menu1.RegisterItem(new CreateItem,3);
  241.     Menubar.DrawmenuBar;
  242.     ! -- Let the Event scheduler take control -- ;
  243.     theMGR.run;
  244. end
  245.  
  246.  
  247.